package com.ffcs.web.controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ffcs.web.model.dto.BaseResponse;
import com.ffcs.web.model.entity.Counselor;
import com.ffcs.web.model.entity.Message;
import com.ffcs.web.model.entity.SystemMessage;
import com.ffcs.web.model.entity.TucaoAnswer;
import com.ffcs.web.model.entity.WxUser;
import com.ffcs.web.services.CounselorService;
import com.ffcs.web.services.MessageService;

@Controller
public class MessageController {

	private Logger logger = Logger.getLogger(MessageController.class);
	
	@Autowired
	private MessageService messageService;
	@Autowired
	private CounselorService counselorService;
	
	/**
	 * 获取我的消息
	 * @param request
	 * @param response
	 * @param sendType
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("getMessage")
	public void getMessage(HttpServletRequest request,HttpServletResponse response,String sendType) throws ServletException, IOException{
		List<Message> messageList=new ArrayList<Message>();
		WxUser wu=(WxUser) request.getSession().getAttribute("wxUser");
		List<TucaoAnswer> tucaoAnswerList=messageService.getTucaoAnswer(wu.getId());
		List<SystemMessage> sysMessageList=null;
		if(sendType.equals("1")){
			String counseTel=(String) request.getSession().getAttribute("counseTel");
			Counselor counselor=counselorService.getCounselor(counseTel);
			sysMessageList=messageService.getSysMessageList(counselor.getId(),sendType);
		}else{
			sysMessageList=messageService.getSysMessageList(wu.getId(),sendType);
		}
		
		for (SystemMessage systemMessage : sysMessageList) {
			Message message=new Message();
			message.setSendContent(systemMessage.getContent());
			message.setCreateDate(systemMessage.getCreateDate());
			message.setFlag("2");
			messageList.add(message);
		}
		
		messageList.addAll(messageService.getMessage(sendType));
		
		for (TucaoAnswer tucaoAnswer : tucaoAnswerList) {
			Message message=new Message();
			message.setSendContent(tucaoAnswer.getAnswerContent());
			message.setCreateDate(tucaoAnswer.getAnswerDate());
			message.setFlag("1");
			message.setTucao(messageService.getTucao(tucaoAnswer.getTucaoId()));
			messageList.add(message);
		}
		request.setAttribute("messageList",messageList);
		
		if(sendType.equals("1")){
			String counseTel=(String) request.getSession().getAttribute("counseTel");
			Counselor counselor=counselorService.getCounselor(counseTel);
			messageService.removeRed(counselor.getId(),sendType);
		}else{
			messageService.removeRed(wu.getId(),sendType);
		}
		request.getRequestDispatcher("mymessage.jsp").forward(request, response);
	}
	
	@RequestMapping("hasMessage")
	@ResponseBody
	public BaseResponse hasMessage(String type,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		BaseResponse baseResponse=new BaseResponse();
		boolean b=false;
		if(type.equals("1")){
			String counseTel=(String) request.getSession().getAttribute("counseTel");
			Counselor counselor=counselorService.getCounselor(counseTel);
			b=messageService.hasMes(counselor.getId(),"1");
		}else{
			WxUser wu=(WxUser) request.getSession().getAttribute("wxUser");
			b=messageService.hasMes(wu.getId(),"2");
		}
		if(!b){
			baseResponse.setResultCode("-1");
			baseResponse.setResultDesc("without");
			return baseResponse;
		}else{
			baseResponse.setResultCode("0");
			baseResponse.setResultDesc("success");
			return baseResponse;
		}
	}
	
}
